package com.account.book.service.impl;

import com.account.book.common.constant.UploadPathConstant;
import com.account.book.common.exception.BasicInfoException;
import com.account.book.common.exception.BasicInfoStatusEnum;
import com.account.book.service.UploadFileService;
import com.account.book.util.CosClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author liang-hw
 * @className UploadFileServiceImpl
 * @description 文件上传实现类
 * @date 2023/11/09 15:04:14
 */
@Service
public class UploadFileServiceImpl implements UploadFileService {
    private static final Logger logger = LoggerFactory.getLogger(UploadFileServiceImpl.class);
    @Autowired
    private CosClientUtil cosClientUtil;

    /**
     * @param @param uploadFile
     * @return @return {@link List }<{@link String }>
     * @author liang-hw
     * @description 文件上传 type 1-账本
     * @date 2023/11/09 15:04:28
     */
    @Override
    public String upload(MultipartFile uploadFile, Integer type) {
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        String path = "";
        if (type == 1) {
            path = UploadPathConstant.ACCOUNT_BOOK_PATH + "/" + formattedDate;
        } else if (type == 2) {
            path = UploadPathConstant.USER_PATH + "/" + formattedDate;
        }
        String url = cosClientUtil.upLoadFile(uploadFile, path);
        if (StringUtils.isEmpty(url)) {
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(),
                    "文件上传失败");
        }
        return url;
    }
}
